﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CopySniffer.Search.Properties;
using System.Runtime.Serialization;

namespace CopySniffer.Search
{
    [DataContract]
    public sealed class SearchToken : IEquatable<SearchToken>
    {
        public SearchToken()
        {
        }

        public SearchToken(string contents)
            : this(contents, false)
        {
        }

        public SearchToken(string contents, bool exactMatch)
            : this(contents, exactMatch, false)
        {
        }

        public SearchToken(string contents, bool exactMatch, bool negated)
        {
            Contents = contents;
            ExactMatch = exactMatch;
            Negated = negated;
        }

        [DataMember]
        public string Contents { get; set; }

        [DataMember]
        public bool ExactMatch { get; set; }

        [DataMember]
        public bool Negated { get; set; }

        public override string ToString()
        {
            return string.Format(
                Resources.SearchTokenToStringFormat,
                Negated ? Resources.NegatedTokenSign : null,
                ExactMatch ? Resources.ExactMatchSign : null,
                Contents);
        }

        public override bool Equals(object obj)
        {
            SearchToken other = obj as SearchToken;
            if (other != null)
            {
                return this.Equals(other);
            }
            return false;
        }

        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }

        #region IEquatable<SearchToken> Members

        public bool Equals(SearchToken other)
        {
            if (other == null)
            {
                return false;
            }
            else if (Object.ReferenceEquals(this, other))
            {
                return true;
            }
            else
            {
                return (this.ExactMatch == other.ExactMatch)
                    && (this.Negated == other.Negated)
                    && (String.Equals(this.Contents, other.Contents, StringComparison.InvariantCulture));
            }
        }

        #endregion

    }
}
